using Microsoft.AspNetCore.Mvc.Filters;
using Vin.Extension.Framework.Models;
using Vin.Extension.Framework.Tools;
using Vin.Tool.Domain.ExceptionModel;

namespace Vin.Extension.Framework.ActionFilter;

public class VinJwtRepeatActionAttribute(string repeatKey = "") : Attribute, IAsyncActionFilter
{
    public string RepeatKey { get; set; } = repeatKey;

    public string? TokenPrefix { get; set; }

    public string ErrorMsg { get; set; } = "系统繁忙，请稍后再试";

    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        var model = VinTokenModelTool.GetTokenModel(TokenPrefix);
        if (model != null && await VinJwtRepeatTool.ExistsAsync(RepeatKey, model.RealUserId!.Value.ToString()))
        {
            throw new BusinessException(ErrorMsg);
        }

        await next();
    }
}